package dice.sinanya.dice.manager;

import dice.sinanya.db.notice.InputNotice;
import dice.sinanya.db.notice.SelectNotice;
import dice.sinanya.entity.EntityNotice;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.NotAdminPowerException;
import dice.sinanya.exceptions.OnlyManagerException;
import dice.sinanya.system.MessagesTag;
import org.nlpcn.commons.lang.util.StringUtil;

import java.util.ArrayList;

import static dice.sinanya.system.MessagesSystem.loginInfo;
import static dice.sinanya.system.MessagesTag.TAG_NOTICE_REFUSE;
import static dice.sinanya.tools.getinfo.GetAdminList.getAdminList;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;

public class Notice {
    private final EntityTypeMessages entityTypeMessages;
    SelectNotice selectNotice = new SelectNotice();
    InputNotice inputNotice = new InputNotice();

    public Notice(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void input() throws NotAdminPowerException {
        checkPower();
        String tag = MessagesTag.TAG_NOTICE_INPUT;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2)).replaceAll("^\n+", "").replaceAll("\n+$", "");
        String title = "";
        ArrayList<String> info = new ArrayList<>();
        String[] input = msg.split("\n");
        if (msg.contains("\n") && input.length > 1) {
            title = input[0].replaceAll("\n", "").replaceAll("\r", "");
            for (int i = 0; i < input.length; i++) {
                if (i != 0) {
                    info.add(input[i]);
                }
            }
        }
        inputNotice.insertNotice(new EntityNotice(entityTypeMessages.getFromGroup(), entityTypeMessages.getFromQq(), title, StringUtil.joiner(info, "\n")));
        sender(entityTypeMessages, stringFormatById(entitySystemProperties.getNoticeInsert(entityTypeMessages.getFromGroupString()), "[CQ:at,qq=" + entityTypeMessages.getFromQqString() + "]", stringFormatById("[ %s ]", title)));
    }

    public void show() throws NotAdminPowerException {
        checkPower();
        ArrayList<EntityNotice> notices = selectNotice.FindNoticeByGroupId(entityTypeMessages.getFromGroup());
        if (notices.size() == 0) {
            sender(entityTypeMessages, entitySystemProperties.getNoticeEmpty(entityTypeMessages.getFromGroupString()));
            return;
        }
        ArrayList<String> result = new ArrayList<>();
        result.add(entitySystemProperties.getNoticeShow(entityTypeMessages.getFromGroupString()));

        for (EntityNotice notice : notices) {
            result.add("提交人:\t" + notice.getQqId() + "\n公告名:\t" + notice.getTitle() + "\n公告内容:\n" + notice.getInfo());
        }
        sender(entityTypeMessages, StringUtil.joiner(result, "\n\n"));
    }

    public void agree() throws NotAdminPowerException, OnlyManagerException {
        checkPower();
        checkManager();
        String tag = MessagesTag.TAG_NOTICE_AGREE;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        boolean exist = selectNotice.CheckNoticeInGroup(entityTypeMessages.getFromGroup(), msg);
        if (exist) {
            EntityNotice notice = selectNotice.getNoticeByGroupId(entityTypeMessages.getFromGroup(), msg);
            inputNotice.deleteNotice(entityTypeMessages.getFromGroup(), msg);
            entityTypeMessages.getMsgSender().SENDER.sendGroupNotice(entityTypeMessages.getFromGroupString(), notice.getTitle(), notice.getInfo());
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(String.valueOf(notice.getQqId()), stringFormatById(entitySystemProperties.getNoticeAgree(entityTypeMessages.getFromGroupString()), notice.getGroupId(), stringFormatById("[ %s ]", notice.getTitle())));
        } else {
            sender(entityTypeMessages, entitySystemProperties.getNoticeNotFound(entityTypeMessages.getFromGroupString()));
        }
    }

    public void refuse() throws NotAdminPowerException, OnlyManagerException {
        checkPower();
        checkManager();
        String tag = TAG_NOTICE_REFUSE;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        boolean exist = selectNotice.CheckNoticeInGroup(entityTypeMessages.getFromGroup(), msg);
        if (exist) {
            EntityNotice notice = selectNotice.getNoticeByGroupId(entityTypeMessages.getFromGroup(), msg);
            inputNotice.deleteNotice(entityTypeMessages.getFromGroup(), msg);
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(String.valueOf(notice.getQqId()), stringFormatById(entitySystemProperties.getNoticeRefuse(entityTypeMessages.getFromGroupString()), notice.getGroupId(), stringFormatById("[ %s ]", notice.getTitle()), entityTypeMessages.getFromQqString()));
        } else {
            sender(entityTypeMessages, entitySystemProperties.getNoticeNotFound(entityTypeMessages.getFromGroupString()));
        }
    }

    private void checkPower() throws NotAdminPowerException {
        ArrayList<String> adminList = getAdminList(entityTypeMessages.getMsgSender(), entityTypeMessages.getFromGroupString());
        adminList.add(entityTypeMessages.getMsgSender().GETTER.getGroupInfo(entityTypeMessages.getFromGroupString()).getOwnerQQ());
        if (!adminList.contains(String.valueOf(loginInfo.getLoginId()))) {
            throw new NotAdminPowerException(entityTypeMessages);
        }
    }

    private void checkManager() throws OnlyManagerException {
        ArrayList<String> adminList = getAdminList(entityTypeMessages.getMsgSender(), entityTypeMessages.getFromGroupString());
        adminList.add(entityTypeMessages.getMsgSender().GETTER.getGroupInfo(entityTypeMessages.getFromGroupString()).getOwnerQQ());
        if (!adminList.contains(entityTypeMessages.getFromQqString())) {
            throw new OnlyManagerException(entityTypeMessages);
        }
    }
}
